package com.example.controller;

import com.example.bean.Author;
import com.example.bean.Person;
import com.example.services.ServicesTest;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mengdexuan on 2017/2/28 15:28.
 *
 * 说明：
 *  1)返回到指定页面，则需要用 @Controller配合视图解析器InternalResourceViewResolver才行
 	2)如果需要返回JSON，XML或自定义mediaType内容到页面显示，需要在对应的方法上加上@ResponseBody注解。
 *
 */
//@RestController //@RestController注解相当于@ResponseBody ＋ @Controller合在一起的作用。
@Controller
@EnableConfigurationProperties({Author.class})
public class ControllerTest {

	private static Logger log = LoggerFactory.getLogger(ControllerTest.class);


	//注入属性值，application.properties配置文件
	@Value("${book.author}")
	private String bookAuthor;
	@Value("${book.name}")
	private String bookName;

	@Autowired
	private Author author;//用 Bean 封装自定义配置文件

	@Autowired
	private ServicesTest servicesTest;

	@RequestMapping("/model")
	public String index(Model model){

		Person p1 = new Person("小明",12);

		Person p2 = new Person("小王",22);
		Person p3 = new Person("小李",32);
		Person p4 = new Person("小张",42);

		List<Person> people = new ArrayList<>();
		people.add(p2);
		people.add(p3);
		people.add(p4);

//		jfinal查询
		Page<Record> result = Db.paginate(1, 3, "select *", "from person");
		String str = JsonKit.toJson(result);
		System.out.println(str);

		model.addAttribute("singlePerson",p1);
		model.addAttribute("people",people);

		return "index";
	}



	@RequestMapping("/test")
	@ResponseBody
	public String index2(){
		String test = "bookAuthor : "+bookAuthor;
		test += "	bookName : "+bookName;

		test += "\n";
		test += author.toString();
		test += "\n";
		test += servicesTest.getStr();

		test += "asdf好好";

		log.info(test);


		return "Hello World SpringBoot! "+test;
	}



















}
